<template>
  <div class="container">
    <div class="data-box" style="height: 540px;">
      <!-- 搜索框 -->
      <MySearch @parent-searchBtn="searchBtn"></MySearch>

      <div class="add" style="width:100px;height:40px; float: right; margin-bottom: -10px;">
        <el-button size="mini" type="primary" @click="handleAdd()" style="width: 80px">
          新增
        </el-button>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="用户 ID">
                <span>{{ props.row.userId }}</span>
              </el-form-item>
              <el-form-item label="用户类型">
                <span>{{ props.row.userType }}</span>
              </el-form-item>
              <el-form-item label="用户名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="用户电话">
                <span>{{ props.row.phone }}</span>
              </el-form-item>
              <el-form-item label="用户年龄">
                <span>{{ props.row.age }}</span>
              </el-form-item>
              <el-form-item label="用户邮箱">
                <span>{{ props.row.email }}</span>
              </el-form-item>
              <el-form-item label="用户密码">
                <span>{{ props.row.password }}</span>
              </el-form-item>
              <el-form-item label="用户地址">
                <span>{{ props.row.address }}</span>
              </el-form-item>
              <el-form-item label="用户星数">
                <span>{{ props.row.star }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="用户 ID" prop="userId">
        </el-table-column>
        <el-table-column label="用户类型" prop="userType">
        </el-table-column>
        <el-table-column label="用户名称" prop="name">
        </el-table-column>
        <el-table-column label="爱心指数" prop="star">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页按钮 -->
    <div class="pagination-box">
      <my-pagination
        style="text-align: right; margin-right: 100px"
        @parent-getData="getData"
        :dataCount="dataCount"
      ></my-pagination>
    </div>

    <!-- 编辑弹出框 -->
    <div class="container" style="display: inline-block">
      <!-- Form -->
      <el-dialog :title="editBoxTitle" :visible.sync="dialogFormVisible">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="用户类型" label-width="68px">
            <el-input v-model="formInline.userType"></el-input>
          </el-form-item>
          <el-form-item label="用户名" label-width="68px">
            <el-input v-model="formInline.name"></el-input>
          </el-form-item>
          <el-form-item label="用户电话" label-width="68px">
            <el-input v-model="formInline.phone"></el-input>
          </el-form-item>
          <el-form-item label="用户年龄" label-width="68px">
            <el-input v-model="formInline.userId"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱">
            <el-input v-model="formInline.email"></el-input>
          </el-form-item>
          <el-form-item label="用户密码">
            <el-input v-model="formInline.password"></el-input>
          </el-form-item>
          <el-form-item label="用户地址">
            <el-input v-model="formInline.address"></el-input>
          </el-form-item>
          <el-form-item label="爱心指数">
            <el-input v-model="formInline.star"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancelBtn()">取 消</el-button>
          <el-button type="primary" @click="handleConfirmBtn()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 编辑弹出框多的end -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageNum: 1,
      dataCount: 0,  // 获取数据总条数
      tableData: [{
        userId: "1",
        userType: "普通用户",
        name: "香茗",
        phone: "123",
        age: "19",
        email: "123@qq.com",
        password: "123456",
        address: "西樵",
        star: "5"
      }],
      dialogFormVisible: false, //编辑框弹出标志
      formInline: {   // 编辑框数据绑定
        userId: "",
        userType: "",
        name: "",
        phone: "",
        age: "",
        email: "",
        password: "",
        address: "",
        star: ""
      },
      editBoxTitle: '修改用户信息'  // 编辑框的标题
    }
  },
  mounted() {
    this.getData(this.pageNum, 5);
    this.getDataCount();
  },
  methods: {
    handleAdd() {
      this.editBoxTitle = '添加用户信息'
      this.dialogFormVisible = true
      this.formInline = {}
    },
    handleEdit(data) {
      this.editBoxTitle = '修改用户信息'
      this.dialogFormVisible = true;
      this.initFormInline(data);
    },
    handleDelete(index, itemData) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        // 执行删除
        this.onDelData(itemData.userId);
      }).then(() => {
        this.tableData.splice(index, 1)
        this.getData(this.pageNum, 5)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    handleCancelBtn(){
      this.dialogFormVisible = false
      this.formInline = {}
    },
    handleConfirmBtn(){
      if(this.editBoxTitle == '修改用户信息'){
        this.onUpdate()
      } else {
        this.onAddData()
      }
    },
    // 根据页码发起请求获取数据
    getData(pageNum, pageSize) {
      // 分页组件调用此方法时，更新当前页码
      this.pageNum = pageNum;
      this.$axios
        .get("user/getByPage?pageNum=" + pageNum + "&pageSize=" + pageSize)
        .then((result) => {
          this.tableData = result.data;
        })
        .catch((err) => {
          alert(err);
        });
    },
    // 获取数据的总条数
    getDataCount() {
      this.$axios
        .get("user/getDataCount")
        .then((result) => {
          this.dataCount = result.data;
        })
        .catch((err) => {
          alert(err);
        });
    },
    onAddData(){
      this.$axios.post("user/insertUser", {
        userType: this.formInline.userType,
        name: this.formInline.name,
        phone: this.formInline.phone,
        age: this.formInline.age,
        email: this.formInline.email,
        password: this.formInline.password,
        address: this.formInline.address,
        star: this.formInline.star
      }).then((result) => {
        if (result.data == 1) {
            this.dialogFormVisible = false;
            // 添加数据后刷新数据
            this.getData(this.pageNum, 5);
            this.getDataCount()
            this.$message({
              showClose: true,
              message: "添加成功！",
              type: "success",
            });
        }
        this.formInline = {}
      }).catch((err) => {
        alert(err)
      });
    },
    onDelData(userId){
      this.$axios.get("user/deleteUserById?userId=" + userId)
      .then((result) => {
        if(result.data == 1){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        } else {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'warning'
          });
        }
      }).catch((err) => {
        this.$message({
            showClose: true,
            message: '错误信息：' + err,
            type: 'warning'
        });
      });
    },
    onUpdate(){
      this.$axios
        .post("user/updateUserById", {
          userId: this.formInline.userId,
          userType: this.formInline.userType,
          name: this.formInline.name,
          phone: this.formInline.phone,
          age: this.formInline.age,
          email: this.formInline.email,
          password: this.formInline.password,
          address: this.formInline.address,
          star: this.formInline.star
        })
        .then((result) => {
          if (result.data == 1) {
            this.dialogFormVisible = false;
            // 编辑数据后刷新数据
            this.getData(this.pageNum, 5);
          }
        })
        .then(() => {
          this.$message({
            showClose: true,
            message: "更新成功！",
            type: "success",
          });
        })
        .catch((err) => {
          alert(err);
        });
    },
    initFormInline(data){
      let newForm = {
        userId: data.userId,
        userType: data.userType,
        name: data.name,
        phone: data.phone,
        age: data.age,
        email: data.email,
        password: data.password,
        address: data.address,
        star: data.star
      };
      this.formInline = newForm;
    },
    searchBtn(value){
      if(value != ''){
        let newTable = this.tableData.filter(
          item => item.name.toLowerCase().includes(value.toLowerCase())
        )
        this.tableData = newTable;
      } else {
        this.getData(this.pageNum, 5)
      }
    }
  },
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 30%;
}
</style>